#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>
#include <iostream>
#include <string>
#include <cv.h>
#include <highgui.h>

// Platform specific part
#ifdef unix
    #include <sys/stat.h>
#else
    #include <direct.h>
#endif

int camara(){
    
    CvCapture* capture = cvCaptureFromCAM(0);
    IplImage *frame = 0;
    CvVideoWriter *writer = 0;
    int i=0,j=0,k=0;

    int isColor = 1;
    int fps     = 25;  // or 30
    int frameW  = 640; // 744 for firewire cameras
    int frameH  = 480; // 480 for firewire cameras
    char buffer[10];

    writer=cvCreateVideoWriter("out.avi",CV_FOURCC('D','I','V','X'),fps,cvSize(frameW,frameH),isColor);
    cvNamedWindow( "result", 1 );

 #ifdef unix
            mkdir("dosya",0755);
#else
            mkdir("dosya");
#endif
  if (capture){
          for(;;){
            if( !cvGrabFrame( capture ))
                break;
            
            frame = cvRetrieveFrame( capture );
            cvWriteFrame(writer,frame);
            if(k>499){
            k=0;
            j++;    
            char name1[]="dosya";
            
            //itoa is not ansi 
            sprintf(buffer,"%d",j);
            strcat(name1,buffer);

            rename("dosya",name1);
#ifdef unix
            mkdir("dosya",0755);
#else
            mkdir("dosya");
#endif
            }
            k++;
            char name[100]="dosya";
            //strcpy(name,name1);
            strcat(name,"/resim");

            sprintf(buffer,"%d",i);
            strcat(name,buffer);
            strcat(name,".jpg");
            cvSaveImage(name,frame);
            i++;
         //int   fps = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
         //   printf("fps:%d\n",fps);
            if( !frame )
                break;
          cvShowImage( "result", frame );
            if( cvWaitKey( 5 ) >= 0 )
                break;
            printf("%s\n",name);
           }       
      }
        cvReleaseImage( &frame );
        cvReleaseCapture( &capture );
        cvReleaseVideoWriter(&writer);
        cvDestroyWindow("result");

    return 0;
    }
